﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  public class MsSqlFilterExpressionFormatter : SqlFilterExpressionFormatter
  {
    #region Constructors

    public MsSqlFilterExpressionFormatter(
      FilterExpressionBase expression,
      DataSourceProvider dataSourceProvider )
      : base( expression, dataSourceProvider )
    {
    }

    #endregion

    protected override void Initialize()
    {
      m_parameterNumber = 0;

      base.Initialize();
    }

    protected override string GetFormat( FilterExpression expression )
    {
      switch( expression.FilterOperator )
      {
        case FilterOperator.StartsWith:
          {
            if( expression.IsCaseSensitive.GetValueOrDefault( true ) )
              return "{1}Left({0}, Len({2})) = {2}";

            return "{1}Left(Lower({0}), Len({2})) = Lower({2})";
          }

        case FilterOperator.EndsWith:
          {
            if( expression.IsCaseSensitive.GetValueOrDefault( true ) )
              return "{1}Right({0}, Len({2})) = {2}";

            return "{1}Right(Lower({0}), Len({2})) = Lower({2})";
          }

        case FilterOperator.Contains:
          {
            if( expression.IsCaseSensitive.GetValueOrDefault( true ) )
              return "{1}CharIndex({2}, {0}) > 0";

            return "{1}CharIndex(Lower({2}), Lower({0})) > 0";
          }

        case FilterOperator.ContainedIn:
          {
            if( expression.IsCaseSensitive.GetValueOrDefault( true ) )
              return "{1}CharIndex({0}, {2}) > 0";

            return "{1}CharIndex(Lower({0}), Lower({2})) > 0";
          }

        default:
          break;
      }

      return base.GetFormat( expression );
    }

    protected override string GetFilterOperator( FilterExpression expression )
    {
      switch( expression.FilterOperator )
      {
        case FilterOperator.StartsWith:
        case FilterOperator.EndsWith:
        case FilterOperator.Contains:
        case FilterOperator.ContainedIn:
          return string.Empty;

        default:
          break;
      }

      return base.GetFilterOperator( expression );
    }

    protected override string CreateNewParameterId()
    {
      m_parameterNumber++;

      return string.Format( "@param{0}", m_parameterNumber );
    }

    protected override string CreateNewFieldId()
    {
      m_fieldNumber++;

      return string.Format( "@field{0}", m_fieldNumber );
    }

    #region Private Fields

    private int m_parameterNumber;
    private int m_fieldNumber;

    #endregion
  }
}
